class Solution(object):
    def equalDigitFrequency(self, s):
        n = len(s)
        pre = [[0 for _ in range(10)] for _ in range(n)]
        pre[0][int(s[0])] += 1
        ans = {s[0]}
        for i in range(1, n):
            for j in range(10):
                pre[i][j] = pre[i - 1][j] + (int(s[i]) == j)
            for j in range(i + 1):
                if j == 0:
                    nums = pre[i]
                else:
                    nums = [pre[i][q] - pre[j - 1][q] for q in range(10)]
                t = set(nums)
                if len(t) == 1 or (len(t) == 2 and 0 in t):
                    ans.add(s[j: i + 1])
        return len(ans)

data = Solution()
s = "1212"
print(data.equalDigitFrequency(s))